/*
*登录页面逻辑
*author:yuzhiyi
*date:2018/08/05
*/

/*
如果IDE没有Js的提示建议使用Sublime
在dom就绪的时候执行(http://jquery.cuishifeng.cn/ready.html)
*/
jQuery(document).ready(function($) {

    var LOGIN_API_URL = "http://127.0.0.1:9091/auth/api/login";

    $.ajaxSetup({
        xhrFields: {withCredentials: true}
    })

    /*login接口begin*/
    function login(params){
        $.ajax({
            url:  LOGIN_API_URL,
            type: 'POST',
            async:true,
            xhrFields:{
                withCredentials:true
            },
            data: params,
            success:function(data){
                console.log(data)
            //如果登录成功跳转到主页
            if (data.code == 200) {
                window.location.href = "/test/home"
            }
            //如果错误就提示相关信息
            else{
                var code = data.code;
                var message = "登录失败:";
                switch (code) {
                    case 10006:
                        message += "该用户不存在";
                        break;
                    case 10008:
                        message += "密码错误";
                        break;
                    default:
                        message = "登录服务异常"
                        break;
                }
                //alert可以做的漂亮一点的，后期迭代一下
                alert(message)
                }
            }
    })}
    /*login接口end */


    /*登录按钮事件注册begin*/
    $('#login-button').click(function(event) {
        /* Act on the event */
        var userType = $("input[name='userType']:checked").val()
        var email = $('#signin-email').val();
        var password = $('#signin-password').val();
        var params = {
            'email':email,
            'password':password
        }
        if(userType=="admin")
            login(params)
        else if(userType=="doctor")
            window.location.href = "/doctor/doctorInfo"
    });
    /*登录按钮事件注册end*/



});
